package com.tb.attendancesystem.teacher;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.client.android.Utils.ZXingUtils;
import com.tb.attendancesystem.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * 教师产生二维码的结果页面
 */

public class ResultActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String TAG = ResultActivity.class.getSimpleName();

    private TextView tvResult;
    private Button btBack;
    private ImageView ivResult;
    private int refreshRate = 10000;                //自动刷新时间间隔
    private boolean activityIsClosed = false;

    private String courseId;
    private int courseTimeCount;                    //记录点到次数
    private String times;                           //记录点到次数的string


    //加载资源
    private void loadRes() {
        courseId = getIntent().getStringExtra("cid");
        courseTimeCount = getIntent().getIntExtra("times", 0);
        //一位数用0填充
        times = ((courseTimeCount < 10) ? "0" : "") + courseTimeCount;
        tvResult = (TextView) findViewById(R.id.tv_result);
        btBack = (Button) findViewById(R.id.button_back);
        ivResult = (ImageView) findViewById(R.id.iv_result);
    }

    //设置监听
    private void register() {
        btBack.setOnClickListener(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t_result);
        loadRes();
        register();
        createQRCode();
    }

    //产生二维码
    private void createQRCode() {
        //自动刷新
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!activityIsClosed) {
                    showQRCode(courseId, times);
                    try {
                        Thread.sleep(refreshRate);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //当活动关闭时应当使数据库的qrcode置空
                //// TODO: 2016/11/17 当活动关闭时应当使数据库的qrcode置空
            }
        });
        thread.start();

    }

    //显示二维码
    private void showQRCode(String courseId, String times) {
        String url = getResources().getString(R.string.host_url) + "/AttendanceSystem/CreateQRCodeString";
        OkHttpUtils
                .get()
                .url(url)
                .addParams("cid", courseId)
                .addParams("times", times)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.d("response", "服务器返回的二维码内容是：" + response);
                        Bitmap qrCodeBitmap = ZXingUtils.getBitmapByString(response,800);
                        ivResult.setImageBitmap(qrCodeBitmap);
                        tvResult.setText(response);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityIsClosed = true;
    }

    @Override
    public void onClick(View v) {
        if (v.equals(btBack)) {
            finish();
        }
    }
}
